﻿using B.S.ProductionProject.Domain;
using B.S.ProductionProject.ErrorCode;
using B.S.ProductionProject.Read.WebAPI.Applications.Commands.ProductionPlanningCommand;
using MediatR;
using SqlSugar;

namespace B.S.ProductionProject.Read.WebAPI.Applications.CommandHandlers.ProductionPlanningCommandHandler
{
    public class GetBYIdProductionPlanningCommandHandler : IRequestHandler<GetBYIdProductionPlanningCommand, APIResult<List<ProductionPlanning>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IConfiguration configuration;
        private readonly ILogger<GetBYIdProductionPlanningCommandHandler> logger;

        public GetBYIdProductionPlanningCommandHandler(ISqlSugarClient db, IConfiguration configuration, ILogger<GetBYIdProductionPlanningCommandHandler> logger)
        {
            this.db = db;
            this.configuration = configuration;
            this.logger = logger;
        }

        public async Task<APIResult<List<ProductionPlanning>>> Handle(GetBYIdProductionPlanningCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ProductionPlanning>> result = new APIResult<List<ProductionPlanning>>();
            result.code = APIResultCode.success;
            result.msg = "查询成功";
            logger.LogInformation("查询计划成功");
            var list = await db.Queryable<ProductionPlanning>().Where(p => p.isDelete == false && p.ProductionPlanningId == request.ProductionPlanningId).ToListAsync();

            result.data = list;
            return await Task.FromResult(result);
        }
    }
}
